

//
//  ArrayViewController.swift
//  Swift 3.0 基础语法
//
//  Created by YYY on 2017/7/17.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

class ArrayViewController: CommonViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    //MARK: - 数组的定义
    func demo18()
    {
        
        let array = ["明明","小敏","nihao"]
        print("array= \(array)")
        
        //swift中基本数据类型不需要包装
        let array1 = [1,12,200,98]
        print("array1= \(array1)")
        
        let p = CGPoint(x: 19, y: 29)
        let array2 = [p]
        print("array2= \(array2)")
        
        //混合数组
        
        let array3 = ["2","nihao","小米",NSValue(cgPoint: p)] as [Any]
        print("array3= \(array3)")
        
        let array4 = [12,98,"小猫","nihaoma"] as [Any]
        print("array4= \(array4)")
    }
    
    //MARK: - 数组的遍历
    func demo19()
    {
        //按照下标遍历
        print("for in 按照下标遍历")
        let array = ["明明","小敏","nihao"]
        for i in 0..<array.count
        {
            print(array[i])
        }
        print("for in 遍历元素")
        //for in 遍历元素
        for object in array
        {
            print(object)
            
        }
        print("遍历下标和内容")
        //元组
        for myobject in array.enumerated()
        {
            print(myobject)
            print("\(myobject.offset)  \(myobject.element)")
        }
        
        for (n,s) in array.enumerated()
        {
            // n 索引下巴
            // s 索引内容
            print("\(n)   \(s)")
        }
        
        //反序遍历
        print("反序遍历")
        for string in array.reversed()
        {
            print(string)
        }
        
        print("反序索引和内容")
        for (n,s) in array.enumerated().reversed()
        {
            print("\(n)    \(s)")
        }
        
        for (n,s) in array.reversed().enumerated()
        {
            print("\(n)    \(s)")
        }
    }
    
    
    //Mark - 数组的增删改
    func demo20()
    {
        // var 可变数组
        // let 不可变数组
        //数组的追加
        var array1 = ["12","9","nm"]
        print("数组追加")
        array1.append("86")
        
        print(array1)
        
        //数组元素的修改
        array1[0] = "gou"
        print("数组元素的修改")
        print(array1)
        
        //数组越界
        //print("数组越界")
        //        array1[4] = "nishishui"
        //        print(array1)
        
        //数组的删除
        //删除第二个
        //        array1.remove(at: 2)
        //
        //        //删除第一个
        //        array1.removeFirst()
        //        //删除最后一个
        //        array1.removeLast()
        //
        //        //删除全部
        //        array1.removeAll()
        //删除全部，并保留空间
        array1.removeAll(keepingCapacity: true)
        
        print("\(array1)   \(array1.capacity)")
        
        //定义一个数组，没有初始化
        var tempArray: [Int]
        //数组的初始化
        tempArray = [Int]()
        
        //tempArray.append(12)
        
        print("初始容量：\(tempArray.capacity)")
        for i in 0..<8
        {
            tempArray.append(i)
            print("\(tempArray)   容量：\(tempArray.capacity)")
        }
        
        let array3 = [12,"22","你好","buhao"] as [Any]
        print(array3.capacity)
    }
    
    func demo21()
    {
        //数组的合并
        var array1 = ["1","2","3"]
        let array2 = ["100"]
        
        //要合并数组的类型必须一致
        array1 += array2
        print(array1)
        
        
        var array3: [Any] = ["1","2","3"]
        let array4 = ["100",90] as [Any]
        
        array3 += array4
        
        //元组
        let array5 = (a: "t",b: "ming",c: "ni",d: 100)
        
        print(array5.0)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
